<template>
  <el-dialog
    @close="cancel"
    :title="title"
    :visible.sync="open"
    width="800px"
    class="dialogClass"
    append-to-body
  >

    <template slot="title">
      <div class="modalTitle">
        <img
          src="@/assets/bluelogo/drawerTitle.png"
          alt=""
          class="titleLogo"
        />{{ title }}
      </div>
    </template>
    <el-form
      ref="form"
      :model="formData"
      :rules="rules"
      size="small"
      label-width="135px"
    >
      <el-form-item label="案件名称" prop="caseName">
        <el-input
          v-model.trim="formData.caseName"
          placeholder="请输入案件名称"
          :maxlength="50"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="判决书号" prop="rulingBookNumber">
        <el-input
          v-model.trim="formData.rulingBookNumber"
          placeholder="请输入判决书号"
          :maxlength="50"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="执行案号" prop="executeNumber">
        <el-input
          v-model.trim="formData.executeNumber"
          placeholder="请输入执行案号"
          :maxlength="50"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="案件所属区域" prop="bm">
            <treeselect
              class="treeselect"
              v-model.trim="formData.bm"
              :options="deptOptions"
              :show-count="true"
              placeholder="请选择案件所属区域"
              :normalizer="normalizer"
            /> </el-form-item
        ></el-col>
        <el-col :span="12">
          <el-form-item label="涉案人数" prop="involvedPeople">
            <el-input
              v-model.trim="formData.involvedPeople"
              placeholder="请输入涉案人数"
              maxlength="9"
              clearable
              oninput="value=value.replace(/[^0-9.]/g,'')"
            >
            </el-input>
          </el-form-item> </el-col
        ><el-col :span="12">
          <!-- <el-form-item label="应发放金额(元)" prop="needMoney">
            <el-input
              v-model.trim="formData.needMoney"
              placeholder="请输入应发放金额(元)"
              oninput="value = value.replace(/[^0-9.]/g, '')"
              :maxlength="20"
              clearable
            ></el-input>
          </el-form-item> -->
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="涉案金额(元)" prop="involvedMoney">
            <el-input
              v-model.trim="formData.involvedMoney"
              placeholder="请输入涉案金额"
              :maxlength="15"
              clearable
              oninput="value=value.replace(/[^0-9.]/g,'')"
            >
            </el-input> </el-form-item
        ></el-col>
        <el-col :span="12">
          <el-form-item label="追回损失(元)" prop="recoveryLoss">
            <el-input
              v-model.trim="formData.recoveryLoss"
              placeholder="请输入追回损失"
              :maxlength="15"
              clearable
              oninput="value=value.replace(/[^0-9.]/g,'')"
            >
            </el-input> </el-form-item
        ></el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="判决损失金额(元)" prop="judgmentLossAmount">
            <el-input
              v-model.trim="formData.judgmentLossAmount"
              placeholder="请输入判决损失金额"
              :maxlength="15"
              clearable
              oninput="value=value.replace(/[^0-9.]/g,'')"
            >
            </el-input>
          </el-form-item> </el-col
        ><el-col :span="12">
          <el-form-item label="认定发放金额(元)" prop="confirmedPayment">
            <el-input
              v-model.trim="formData.confirmedPayment"
              placeholder="请输入认定发放金额(元)"
              oninput="value=value.replace(/[^0-9.]/g,'')"
              :maxlength="15"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="案件简介" prop="introduction">
        <el-input
          v-model.trim="formData.introduction"
          type="textarea"
          placeholder="请输入案件简介"
          :maxlength="300"
          show-word-limit
          :autosize="{ minRows: 7 }"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <div class="dialog-footer">
        <el-button @click="cancel" class="resetButton">取消</el-button>
        <el-button class="searchButton" @click="submitForm">确定</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import { MODALTYPE, MODALIMG, CASEDEFAULTSTATE } from "../../../utils/variable";
import { onlyNumber } from "../../../utils/commonMethods";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  getCase,
  addCase,
  getTree,
  validNumber,
  updateCase,
} from "@/api/caseManage/case";
export default {
  inheritAttrs: false,
  components: { Treeselect },
  props: {
    modalType: {
      type: String,
      require: true,
    },
    id: {
      type: Number,
    },
  },
  data() {
    //案件重复校验
    var checkRulingBookNumber = (rule, value, callback) => {
      if (!value) {
        callback(new Error("判决书号不能为空"));
      } else if (this.modalType == MODALTYPE["新增"]) {
        let params = {
          caseName: this.formData.caseName,
          rulingBookNumber: this.formData.rulingBookNumber,
        };
        validNumber(params).then((response) => {
          if (response.data === 1) {
            callback(new Error("该案件已重复！"));
          } else {
            callback();
          }
        });
      } else if (this.modalType == MODALTYPE["修改"]) {
        if (
          this.temCaseName !== this.formData.caseName &&
          this.temRulingBookNumber !== this.formData.rulingBookNumber
        ) {
          let params = {
            caseName: this.formData.caseName,
            rulingBookNumber: this.formData.rulingBookNumber,
          };
          validNumber(params).then((response) => {
            if (response.data === 1) {
              callback(new Error("该案件已重复！"));
            } else {
              callback();
            }
          });
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    return {
      isDisabled: false,
      // 弹出层标题
      title: "",
      temCaseName: "",
      temRulingBookNumber: "",
      // 部门树选项
      deptOptions: [],
      // 是否显示弹出层
      open: false,
      formData: {
        caseName: "",
        rulingBookNumber: "",
        executeNumber: "",
        bm: "",
        involvedPeople: "",
        needMoney: "",
        introduction: "",
        judgmentLossAmount: "",
        confirmedPayment: "",
        recoveryLoss: "",
        involvedMoney: "",
      },
      rules: {
        recoveryLoss: [
          {
            required: true,
            message: "请输入追回损失",
            trigger: "blur",
          },
        ],
        involvedMoney: [
          {
            required: true,
            message: "请输入涉案金额",
            trigger: "blur",
          },
        ],
        judgmentLossAmount: [
          {
            required: true,
            message: "请输入判决损失金额",
            trigger: "blur",
          },
        ],
        caseName: [
          {
            required: true,
            message: "请输入案件名称",
            trigger: "blur",
          },
        ],
        rulingBookNumber: [
          {
            required: true,
            validator: checkRulingBookNumber,
            trigger: "blur",
          },
        ],
        executeNumber: [
          {
            required: true,
            message: "请输入执行案号",
            trigger: "blur",
          },
        ],
        bm: [
          {
            required: true,
            message: "请选择案件所属选择",
            trigger: "change",
          },
        ],
        involvedPeople: [
          {
            required: true,
            message: "请输入涉案人数",
            trigger: "blur",
          },
        ],
        // needMoney: [
        //   {
        //     required: true,
        //     message: "请输入应发放金额(元)",
        //     trigger: "blur",
        //   },
        // ],
        confirmedPayment: [
          {
            required: true,
            message: "请输入认定发放金额",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    resetButtonContent() {
      if (this.modalType == MODALTYPE["详情"]) {
        return "关闭";
      } else {
        return "取消";
      }
    },
  },
  watch: {
    modalType: {
      handler(value) {
        if (value == MODALTYPE["新增"]) {
          this.reset();
          this.initTreeOptions();
          this.open = true;
          this.title = "新增案件";
        } else if (value == MODALTYPE["修改"]) {
          this.reset();
          this.initTreeOptions();
          if (this.id) {
            getCase(this.id).then((response) => {
              this.formData = response.data;
              this.temCaseName = this.formData.CaseName;
              this.temRulingBookNumber = this.formData.rulingBookNumber;
              this.open = true;
              this.title = "修改案件";
            });
          }
        } else if (value == "detail") {
          this.isDisabled = true;
          this.reset();
          if (this.id) {
            getCase(this.id).then((response) => {
              this.formData = response.data;
              this.open = true;
              this.title = "案件详情";
            });
          }
        }
      },
      immediate: true,
    },
  },
  created() {},
  mounted() {},
  methods: {
    onlyNumber,
    inputFunc(val) {
      console.log(val);
      val = val.replace(/[^0-9.]/g, "");
      console.log(val);
      this.formData.involvedPeople = val;
    },
    moneyInputFunc(val) {
      console.log(val);
      val = val.replace(/[^0-9.]/g, "");
      console.log(val);
      this.formData.needMoney = val;
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id, //自定义id
        label: node.label, //自定义label
        //处理children，当children为[]或null时，子节点会展示No sub-options，一般子节点不需要展示这个，所以将children置为0
        children: node.children && node.children.length > 0 ? node.children : 0,
      };
    },
    // 取消按钮
    cancel() {
      this.$emit("update:modalType", "");
      this.open = false;
      this.reset();
    },
    initTreeOptions() {
      getTree().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 表单重置
    reset() {
      this.formData = {
        caseName: "",
        rulingBookNumber: "",
        executeNumber: "",
        caseState: "",
        bm: undefined,
        involvedPeople: "",
        payedPeople: "",
        payedMoney: "",
        needMoney: "",
        introduction: "",
        judgmentLossAmount: "",
        confirmedPayment: "",
        recoveryLoss: "",
        involvedMoney: "",
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.modalType == MODALTYPE["修改"]) {
            updateCase(this.formData).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit("update:modalType", "");
              // this.$parent.getList();
              this.$emit("fatherMethod");
            });
          } else if (this.modalType == MODALTYPE["新增"]) {
            this.formData.caseState = CASEDEFAULTSTATE;
            addCase(this.formData).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit("update:modalType", "");
              // TODO：不要用这种方式访问父组件
              // this.$parent.getList();
              this.$emit("fatherMethod");
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../../../assets/styles/dialog.scss"; //模态框样式

::v-deep.treeselect {
  font-size: 12px !important;
  font-family: $font-family !important;
  font-weight: $form-label-font-weight !important;
}
</style>
